<?php 
include_once "../../Lib/DBMySql.inc";
include_once "../../Lib/Common.php";
include_once "../../Lib/Lib.php";
include_once "../../Lib/Paging.php";
include_once "../../Entities/usersObj.php";
include_once "../../DAL/usersDAL.php";
include_once "../../Lib/Content.php";
include_once "../../Lib/Session.php";

// If user not is administrator/partner manager then he/she can't user this page
if ($userSess->userroleid>2)
{
	header("Location: ../product/list.php");
	exit;	   
}
try
{
	$mainDB = NewDB(DATABASE_HOST, DATABASE_NAME , DATABASE_USER, DATABASE_PASSWORD);
	$title		= "User Listing";
	$pageError	= "";
	$frmUpdate 	= "detail.php";	// this page user for add/edit information
	$frmName 	= "frmMain";		// this is main form's name
	// Delete the selected records
	if (isset($act) && ($act== "DELETE" || $act== "ACTIVE")) 
	{
		//if (!usersDAL::Pure($mainDB,usersObj::idFieldName()."=".ToSQLPostVAL($txtID), $errorMsg))
		//	$pageErro = "Invalid Delete. $error";
		$users = new usersObj();
		$users = usersDAL::Load($mainDB, $txtID, $pageError);
		$users->deleteflag = ($act== "DELETE"?"1":"0");
		usersDAL::Update($mainDB, $users, $pageError);
	}
	
	if (!isset($SortType) || $SortType=="") $SortType = "ASC";
	if (!isset($SortField) || $SortField=="") $SortField = "username";
	$orderStr = "$SortField $SortType";
	
	// Load List with user role
	$whereFilter = "";
	if ($userSess->userroleid>1)
		$whereFilter = "users.partnerid=".$userSess->partnerid;
	// Paging
	$rowCount = RowCount($mainDB,  usersObj::TableName(), $whereFilter, $errorMsg, array('userrole', 'partner'), array('users.userroleid=userrole.id', 'users.partnerid=partner.id'));	
	
	$rowsOfPage	= ROWS_OF_ADMIN_LIST;
	if (!isset($page) || $page<1) $page	= 1;
	$pages	= ceil($rowCount/$rowsOfPage);
	if ($page>$pages && $pages>0) $page = $pages;
	
	$limit = " " .(($page-1)*$rowsOfPage).", ".$rowsOfPage;
	
	$notes = "";
	$paging = Paging($page, $rowCount, $rowsOfPage, $btnNextText, $btnPrevText, $btnGoPageText, "link" /*link class*/, "label" /*link text class*/, "inputNumber" /*input number class*/ , $noted, "document. $frmName");
	
	$returnData = usersDAL::GetList($mainDB, $whereFilter, $orderStr, $limit, $errorMsg, array('userrole'), array('users.userroleid=userrole.id'), array('name'));
	
	if ($errorMsg!="" && $pageError=="") $pageError = "Error: ".$errorMsg;	
	} 
catch(Exception $ex)
{ $pageError = $ex.message;}

include "../header.php";
?>
<form name=<?php  echo $frmName;?> method=post action="<?php  echo $_SERVER['PHP_SELF']; ?>">
<input type=hidden name=act>
<input type=hidden name=page value="<?php echo $page;?>">
<input type=hidden name=pages value="<?php echo $pages;?>">
<input type=hidden name=txtID>
<input type=hidden name=SortField value="<?php  echo $SortField;?>">
<input type=hidden name=SortType value="<?php  echo $SortType;?>">
<table width="99%" border="0" cellspacing="0" cellpadding="0" align="right">
	<tr valign=middle> 
		<td class="titleText" colspan=3 height=25><?php  echo $title;?></td>
	</tr>
	<tr valign=middle> 
		<td colspan=3 height=3 bgcolor="#FFFFFF"></td>
	</tr>
	<?php  
	if ($pageError!="") echo "<tr><td align=center colspan=3 class=\"errorText\">$pageError</td></tr>";
	?>
	<tr> 
		<td align="center" valign="top" colspan=3>
			<TABLE border=0 cellPadding=1 cellspacing="1" width="100%" bgcolor='#bccfdc'>
				<tr class='gridHeader'>
					<td width="5%" height="25" nowrap align="center">#</td>
					
<td width="100" nowrap><a href="#" onClick="_sort('users.username', document.<?php  echo $frmName;?>);" class="adminGridHeaderLink" title="Sort by username"><b>Username</b></a></td>
<td width="50" nowrap><a href="#" onClick="_sort('userrole.name', document.<?php  echo $frmName;?>);" class="adminGridHeaderLink" title="Sort by user type"><b>User Type</b></a></td>
<td width="30" nowrap><a href="#" onClick="_sort('users.firstname', document.<?php  echo $frmName;?>);" class="adminGridHeaderLink" title="Sort by firstname"><b>Firstname</b></a></td>
<td width="70" nowrap><a href="#" onClick="_sort('users.lastname', document.<?php  echo $frmName;?>);" class="adminGridHeaderLink" title="Sort by lastname"><b>Lastname</b></a></td>
					<!--
					<td width="20%" nowrap><a href="#" onClick="_sort('Username', document.<?php  echo $frmName;?>);" class="adminGridHeaderLink" title='Sort by Username'><b>Username</b></a></td>
					<td width="45%" nowrap><a href="#" onClick="_sort('Fullname', document.<?php  echo $frmName;?>);" class="adminGridHeaderLink" title='Sort by Fullname'><b>Fullname</b></a></td>
					<td width="15%" nowrap><a href="#" onClick="_sort('UserType', document.<?php  echo $frmName;?>);" class="adminGridHeaderLink" title='Sort by User type'><b>User type</b></a></td>
					-->
					<td width="15%" nowrap align=center><b>Action</b></td>
				</TR>
			<?php 
			for ($i=0; is_array($returnData) && $i<count($returnData); $i++) {
				$users = $returnData[$i];
			?>
				<tr class="<?php  if (($i%2)==0) echo "gridOddRow"; else echo "gridEvenRow";?>">
					<td align=right><?php echo sprintf("%5d &nbsp; ", (($page-1)*$rowsOfPage) + $i+1);?></td>
					<td align="left"><?php  echo $users->username;?></td>
                    <td align="left"><?php  echo $users->otherFields["userrole_name"];?></td>
                    <td align="left"><?php  echo $users->firstname;?></td>
                    <td align="left"><?php  echo $users->lastname;?></td>
					<td align=left> &nbsp; &nbsp; &nbsp;
							<a href="#" onClick="_update(<?php  echo $users->id;?>, '<?php  echo $frmUpdate;?>', document.<?php  echo $frmName;?>);" class="adminGridRowLink" title="<?php  echo $title_Edit;?>">Update</a>
						<?php if (strtolower($users->username)!="admin") {?>
						| 
						<a href="#" onClick="_<?=($users->deleteflag==0?"inactive":"active")?>(<?php  echo $users->id;?>, document.<?php  echo $frmName;?>);" class="adminGridRowLink" title="<?=($users->deleteflag==0?"Inactive this record.":"Active this record.")?>"><?=($users->deleteflag==0?"Inactive":"Active")?></a>
                        <? } ?>
					</td>
				</TR>
			<?php 
			}
			?>
			</TABLE>
		</td>
	</tr>
	<tr valign=bottom> 
		<td colspan=3 height=10></td>
	</tr>
	<tr> 
		<td width="10%">
			<a href="#" onClick="_add('<?php  echo $frmUpdate;?>',document.<?php  echo $frmName;?>);" title="<?php  echo $title_Add;?>" class="buttonlink"><?php  echo $btnAdd_Value;?></a>
		</td>
		<td width="40%" align=right><?php  echo $notes;?></td>
		<td width="50%" class="paging"><?php  echo $paging;?></td>
	</tr>
</table>
</form>
<?php 
include "../footer.php";
?>